package net.yxsoft.controller.pc.sysmgt.common.interceptor;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import net.yxsoft.enumclass.TsUserTypeEnumCls;
import net.yxsoft.resource.AuthuserRes;
import net.yxsoft.service.infrastructure.LoginService;
import rapid.core.Authuser;

/**
 * 客户端用户类型拦截器
 * 类型限定：type0、type1
 *
 * @author zhaohaibo
 */
public class LoginUserTypeInterceptor implements Interceptor {

  @Override
  public void intercept(Invocation invocation) {
    Controller c = invocation.getController();

    Authuser user = c.getSessionAttr(Authuser.KEY_USER);

    if(user != null) {
      TsUserTypeEnumCls utype = user.get(AuthuserRes.KEY_TYPE);

      if(!utype.equals(TsUserTypeEnumCls.TYPE_0)) {
        String sessionId = c.getCookie(AuthuserRes.KEY_TS_SESSION_UID);

        LoginService.INSTANCE.logout(sessionId);
        c.removeCookie(AuthuserRes.KEY_TS_SESSION_UID);
        c.removeSessionAttr(Authuser.KEY_USER);
      }
    }

    invocation.invoke();
  }
}
